<?php
//! Controller class, which is the backbone of a web page 
/*!
	The controller class handle the actions for the specific web page. It then render the view associated to the controller
*/
class wcController
{
	//!name of the controller
	public $Name;
	//!current action
	public $Action;
	//!the $Data variable is used by the view to provide dynamic rendering, every variable held inside the $Data array can be used easily in the view
	public $Data;
	//!the body of the web page
	public $Body;
	//!each page can have several types
	public $Type;
	
	//!constructor, responsible of initialization, will read the Q parameter if it's used in the URL and will map it to the action. Will also handle the 'language' parameter.
	public function __construct($name='',$type='',$area='')
	{
		$this->Name		= $name;
		$this->Action	= wcCore::varGet('q');
		$this->Data		= array();
		$this->Body		= '';
		$this->Type		= $type;
		$this->Area  	= $area;
		
		$this->Data['LANGUAGE']	=   wcCore::getLanguage($area);
		$this->Data['URLSTATIC']=   wcCore::cacheFetch(SITE.'urlStaticTheme');	//USED BY HTML STATIC CONTENT LINKS
		$this->Data['PATHSITE']	=   wcCore::cacheFetch(SITE.'pathSite');	//USED BY HTML STATIC CONTENT LINKS
		$this->Template			= 'default';
		

	}
	//!load a specific view template from disk
	public function loadTemplate($path='')
	{
		if (strlen($path)==0)	// index.html.fr.php
			$path=sprintf('./views/%s-%s-%s-%s.php', $this->Name, $this->Template, $this->Type ,$this->Data['LANGUAGE']);

		if (file_exists($path) )
		{ 
			ob_start();
			include $path;
			$this->Body = ob_get_contents();
			ob_end_clean();
		}
        else
        {
        
        	//echo "template not found:$path ".' -- GETCWD:'.getcwd();
        	echo "<br />";
        }

	}
	//!this function load the template specified, launch the plugins specified in the templates, insert dynaminc data and flush out the output 
	public function render()
	{
		$this->loadTemplate();
		$this->processPlugins();
		$this->processData();
		echo $this->Body ;
		
		
		/*
function renderPage()
{
	global $ctrl;
	$ctrl->loadTemplate();
	$ctrl->processData();
	$ctrl->processPlugins();
	$ctrl->processData();
	$ctrl->render();
}
*/
	}
	//!this function will search for specific keywords in the template, when keywords are found they are looked up in the $Data array, if they are found, they will be replaced by the actual value
	public function processData()
	{
		$page=$this->Body;
		while (list($key, $val) = each($this->Data))
		{
			$search='<!--[-'.$key.'-]-->';
			$page=str_replace( $search ,$val, $page);
		}
		$this->Body = $page;
	}
	//!this function will search for specific keywords in the template, when keywords are found, we try to call the appropriate plugin and the output results will be sent back to the template
	public function processPlugins()
	{
		$page=$this->Body;
		while($p=strpos($page,'<['))
		{
			$pos=$p+2;
			if ($pe=strpos($page,']>',$pos))
			{
				$pcmd=strpos($page,':',$pos);
				$v=substr($page,$pcmd+1,$pe-$pcmd-1 );
				$args=array();
				$kv=strlen($v);
				$cmd=$val='';
				$bcmd=true;
//				echo "v:$v [$kv]<br />";
				for ($k=0;$k<$kv;$k++)
				{
					$a=$v[$k];
					if (($a=='|') || ($a=="\n")|| ($a=="\r"))
					{
						$args[$cmd]=$val;
						$bcmd=true;
						$cmd=$val='';
						continue;
//						echo sprintf("cmd [%s] val [%s]<br />",$cmd,$val);
					}
					if ($a=='=')
						$bcmd=false;
					else
					{
						if ($bcmd)
							$cmd.=$a;
						else
							$val.=$a;
						
					}
				}
				$args[$cmd]=$val;
//				var_dump($args);
				$cmd= substr($page,$pos, $pcmd-$pos);
			
				//echo "**{$cmd}**".var_dump($args)."**";
				
				$func='plugin__'.$cmd;
						
				$temp=substr($page,0,$pos-2).$func($args).substr($page,$pe+2);
				$page=$temp;
				//$pos=0;
				//str_replace(,,$page);
				
			}
			else
				break;
		}
		$this->Body = $page;
	}
	//!output the content of a specific variable in the $data array 
	public function write($name)
	{
		if (isset($this->Data[$name]))
			echo $this->Data[$name];
	}
}

?>
